package com.st.controller.headend;

import com.st.dao.ICartDao;
import com.st.dao.IOrderDao;
import com.st.dao.IOrderGoodsDao;
import com.st.entity.Cart;
import com.st.entity.Order;
import com.st.entity.OrderGoods;
import com.st.service.IShopOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("so")
public class ShopOrderController {
    @Autowired
    private IShopOrderService shopOrderService;
    @Autowired
    private IOrderGoodsDao orderGoodsDao;
    @Autowired
    private ICartDao cartDao;
    //创建订单方法
    @RequestMapping("createOrder")
    public ModelAndView createOrder(OrderGoods og, HttpServletRequest req){
        HttpSession session=req.getSession();
        Integer userId= (Integer) session.getAttribute("userId");
        String userPhone= (String) session.getAttribute("userPhone");
         Order order = shopOrderService.createOrder(userId,userPhone,og);
         ModelAndView mv=new ModelAndView();
         mv.addObject("order",order);
         mv.addObject("userPhone",userPhone);
         mv.setViewName("/headend/order_pay");
         return mv;
    }
    @RequestMapping("pay")
    public ModelAndView pay(String orderNo, BigDecimal orderMoney, String userPass,HttpServletRequest req){
        ModelAndView mv=new ModelAndView();
          HttpSession session = req.getSession();
          //获取用户id
          Integer userId= (Integer) session.getAttribute("userId");
          //执行支付相关的业务
         Boolean tar= shopOrderService.pay(orderNo,orderMoney,userPass,userId);
        if (tar){
          mv.addObject("msg","支付成功!");
          List<OrderGoods>list3=new ArrayList<>();
            List<OrderGoods> list=orderGoodsDao.query(orderNo);
            OrderGoods orderGoods=new OrderGoods();
            orderGoods.setOrderNo(list.get(0).getOrderNo());
            orderGoods.setGoodsName(list.get(0).getGoodsName());
            orderGoods.setBuyCount(list.get(0).getBuyCount());
            orderGoods.setCreateTime(list.get(0).getCreateTime());
            orderGoods.setTotalMoney(list.get(0).getTotalMoney());
            list3.add(orderGoods);
            mv.addObject("list5",list3);
        }else {
            mv.addObject("msg","支付失败!");
        }
        String userPhone= (String) session.getAttribute("userPhone");
        mv.addObject("orderNo",orderNo);
        mv.addObject("userPhone",userPhone);
        mv.setViewName("/headend/order_pay_msg");
        return mv;
    }
    //订单支付购物车的
    @RequestMapping("payy")
    public ModelAndView payy(@Param("orderNo") String[] orderNo, @Param("orderMoney") BigDecimal[] orderMoney,
                       String userPass, BigDecimal total,HttpServletRequest req) {
        HttpSession session = req.getSession();
        //获取用户id

        Integer userId = (Integer) session.getAttribute("userId");
        String userPhone= (String) session.getAttribute("userPhone");
        Boolean tar = null;
        Boolean tart =null;
        for (int i = 0; i < orderNo.length; i++) {
            tar = shopOrderService.pay(orderNo[i], orderMoney[i], userPass, userId);
        }
        ModelAndView mv = new ModelAndView();
        if (tar) {
            List<OrderGoods> list3=new ArrayList<>();

           for (int i = 0; i < orderNo.length; i++){
               List<OrderGoods> list=orderGoodsDao.query(orderNo[i]);
               OrderGoods orderGoods=new OrderGoods();
               orderGoods.setOrderNo(list.get(0).getOrderNo());
               orderGoods.setGoodsName(list.get(0).getGoodsName());
               orderGoods.setBuyCount(list.get(0).getBuyCount());
               orderGoods.setCreateTime(list.get(0).getCreateTime());
               orderGoods.setTotalMoney(list.get(0).getTotalMoney());
               list3.add(orderGoods);
               System.out.println(list);
               Cart cart=new Cart();
               cart.setUserId(userId);
               cart.setGoodsId(list.get(0).getGoodsId());
               List<Cart> list1=cartDao.query(cart);
             tart= shopOrderService.delCart(list1.get(0));
           }
           if (tart){
               mv.addObject("msg", "支付成功!");
               mv.addObject("list3",list3);
           }

        } else {
            mv.addObject("msg", "支付失败!");
        }
        List<String>list=new ArrayList<>();
        for (int i=0;i<orderNo.length;i++){
            list.add(orderNo[i]);
        }
        mv.addObject("list", list);
        mv.addObject("userPhone",userPhone);
        mv.setViewName("/headend/order_pay_msg");
        return mv;
    }
}
